#!/usr/bin/env python
# coding=UTF-8
'''
@Author: 陈亮
@LastEditors: 陈亮
@GitHub: https://github.com/drinkwd
@Description: '安全报表'
@Date: 2019-04-03 14:48:13
@LastEditTime: 2019-04-26 16:15:12
'''

from flask import Blueprint, g
from psycopg2 import connect
from psycopg2.extras import RealDictCursor
import json

from config import config
from lib import auth
from lib.validform import V, ValidateForm

app = Blueprint(__name__ + "_app", __name__)


# 连接/关闭 数据库
@app.before_request
def setupdb():
    g.conn = connect(**config.DatabaseConfig.siem_stc)
    g.cursor = g.conn.cursor(cursor_factory=RealDictCursor)


@app.teardown_request
def unsetdb(exception):
    if g.cursor:
        g.cursor.close()
        g.cursor = None
    if g.conn:
        g.conn.close()
        g.conn = None


# /api/safetyformlls/submitcondition
@app.route('/api/safetyformlls/submitcondition', methods=['post'])
@auth.permission('safetyForm')
def safetyformlls_submitcondition():
    try:
        form = ValidateForm(
            reportName=['报表名称', V.required()],
            reportType=['报表类型', V.required()],
            dateString=['时间范围', V.optional()],
            level=['事件等级', V.optional()],
            risklevel=['风险等级', V.optional()],
            unit=['所属机构', V.optional()],
            tasktype=['任务类型', V.optional()],
            collection=['任务监测范围', V.optional()],
        )
        (flag, data) = form.validate()
        if not flag:
            data['status'] = 'fail'
            return json.dumps(data)
        strcustomize = ''
        if data['dateString']:
            data['dateString'] = tuple(json.loads(data['dateString']))
        if data['reportName'] == 'attackevent':
            if data['reportType'] == 'customize':
                if not data['unit']:
                    data['unit'] = '9999'
                if not data['level']:
                    data['level'] = '9999'
                if not data['dateString']:
                    strcustomize = ''
                else:
                    strcustomize = "and '%s'<=static_date and static_date<='%s'" % (data['dateString'][0], data['dateString'][1])
                g.cursor.execute("""
                    SELECT substring(static_date::varchar from 0 for 11) as static_date, victim_system_num, victim_assert_num,
                        event_num, add_num, already_analysis_num, not_analysis_num, handle_pre
                    FROM public.t_attackevent_statictis
                    where node_id='%s' and threaten_level='%s' %s order by static_date
                """ % (data['unit'], data['level'], strcustomize))
                data['tableAttack'] = g.cursor.fetchall()
            elif data['reportType'] == 'week':
                g.cursor.execute("""
                    select a.first_data as fromdate, a.lastdate as todate
                    from (
                        select substring(generate_series(date_trunc('week',a.min_static_date), date_trunc('week',a.max_static_date),'1 week')::varchar from 0 for 11) as first_data,
                            substring(d2t(generate_series(date_trunc('week',a.min_static_date), date_trunc('week',a.max_static_date),'1 week')::date+6)from 0 for 11) as lastdate
                        from (
                            select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                                substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                            from t_attackevent_statictis where node_id='9999' and threaten_level='9999'
                        )a
                    )a
                """)
                # print g.cursor.fetchall()
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '攻击事件统计周报'
            elif data['reportType'] == 'month':
                g.cursor.execute("""
                select substring(a.first_data::varchar from 0 for 11) as fromdate,substring(a.lastdate::varchar from 0 for 11) as todate
                from (
                    select generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month') as first_data,
                        generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month')::timestamp + '1 month - 1 day' as lastdate
                    from (
                        select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                            substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                        from t_attackevent_statictis where node_id='9999' and threaten_level='9999'
                    )a
                )a
                """)
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '攻击事件统计月报'
            elif data['reportType'] == 'period':
                g.cursor.execute("""
                select substring(a.first_data::varchar from 0 for 11) as fromdate,substring(a.lastdate::varchar from 0 for 11) as todate
                from (
                    select generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'3 month') as first_data,
                            generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'3 month')::timestamp + '3 month - 1 day' as lastdate
                    from (
                        select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                            substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                        from t_attackevent_statictis where node_id='9999' and threaten_level='9999'
                    )a
                )a
                left join (
                    select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date
                    from t_attackevent_statictis where node_id='9999' and threaten_level='9999'
                )b
                on 1=1
                where a.lastdate>b.min_static_date
                """)
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '攻击事件统计季报'
            else:
                g.cursor.execute("""
                select substring(a.first_data::varchar from 0 for 11) as fromdate,substring(a.lastdate::varchar from 0 for 11) as todate
                from (
                    select generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'1 year') as first_data,
                            generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'1 year')::timestamp + '1 year' as lastdate
                    from (
                        select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                            substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                        from t_attackevent_statictis where node_id='9999' and threaten_level='9999'
                    )a
                )a
                """)
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '攻击事件统计年报'
        elif data['reportName'] == 'riskevent':
            if data['reportType'] == 'customize':
                if not data['risklevel']:
                    data['risklevel'] = '9999'
                if not data['dateString']:
                    strcustomize = ''
                else:
                    strcustomize = "and '%s'<=static_date and static_date<='%s'" % (data['dateString'][0], data['dateString'][1])
                g.cursor.execute("""
                    SELECT substring(static_date::varchar from 0 for 11) as static_date,riskitem_num, add_num, alwarning_num, not_warning_num,
                        handle_pre
                    FROM public.t_riskitem_statistics
                    where risk_event_level='%s' %s order by static_date
                """ % (data['risklevel'], strcustomize))
                data['tableRisk'] = g.cursor.fetchall()
            elif data['reportType'] == 'week':
                g.cursor.execute("""
                    select a.first_data as fromdate, a.lastdate as todate
                    from (
                        select substring(generate_series(date_trunc('week',a.min_static_date), date_trunc('week',a.max_static_date),'1 week')::varchar from 0 for 11) as first_data,
                            substring(d2t(generate_series(date_trunc('week',a.min_static_date), date_trunc('week',a.max_static_date),'1 week')::date+6)from 0 for 11) as lastdate
                        from (
                            select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                                substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                            from t_riskitem_statistics where  risk_event_level='9999'
                        )a
                    )a
                """)
                # print g.cursor.fetchall()
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '风险事项统计周报'
            elif data['reportType'] == 'month':
                g.cursor.execute("""
                select substring(a.first_data::varchar from 0 for 11) as fromdate,substring(a.lastdate::varchar from 0 for 11) as todate
                from (
                    select generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month') as first_data,
                        generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month')::timestamp + '1 month - 1 day' as lastdate
                    from (
                        select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                            substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                        from t_riskitem_statistics where risk_event_level='9999'
                    )a
                )a
                """)
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '风险事项统计月报'
            elif data['reportType'] == 'period':
                g.cursor.execute("""
                select substring(a.first_data::varchar from 0 for 11) as fromdate,substring(a.lastdate::varchar from 0 for 11) as todate
                from (
                    select generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'3 month') as first_data,
                            generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'3 month')::timestamp + '3 month - 1 day' as lastdate
                    from (
                        select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                            substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                        from t_riskitem_statistics where  risk_event_level='9999'
                    )a
                )a
                left join (
                    select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date
                    from t_riskitem_statistics where  risk_event_level='9999'
                )b
                on 1=1
                where a.lastdate>b.min_static_date
                """)
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '风险事项统计季报'
            else:
                g.cursor.execute("""
                select substring(a.first_data::varchar from 0 for 11) as fromdate,substring(a.lastdate::varchar from 0 for 11) as todate
                from (
                    select generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'1 year') as first_data,
                            generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'1 year')::timestamp + '1 year' as lastdate
                    from (
                        select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                            substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                        from t_riskitem_statistics where  risk_event_level='9999'
                    )a
                )a
                """)
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '风险事项统计年报'
        else:
            if data['reportType'] == 'customize':
                strtype = ''
                if data['tasktype'] and data['collection']:
                    strtype = "where aim_id = '%s' and task_type='%s'" % (data['collection'], data['tasktype'])
                elif not data['tasktype'] and data['collection']:
                    strtype = "where aim_id = '%s' " % (data['collection'])
                elif not data['tasktype'] and not data['collection']:
                    strtype = ''
                else:
                    strtype = " where task_type='%s'" % data['tasktype']
                if not data['dateString']:
                    strcustomize = ''
                else:
                    strcustomize = " '%s'<=static_date and static_date<='%s'" % (data['dateString'][0], data['dateString'][1])
                g.cursor.execute("""
                    select count(distinct ti.task_id) as task_num,	--任务数量
                            substring(static_date::varchar from 0 for 11) as static_date, --时间
                            count(distinct case when ti.task_type = '1' then ti.task_id else null end) as deep_analysis_num,	--深度分析数量
                            count(distinct case when ti.task_type = '2' then ti.task_id else null end) as flow_monitor_num,	--流量监测数量
                            count(distinct case when ti.task_type = '3' then ti.task_id else null end) as audit_monitor_num,	--审计检测数量
                            count(distinct case when ti.task_type = '4' then ti.task_id else null end) as frail_monitor_num,	--脆弱性监测数量
                            count(distinct case when ti.task_state = '1' then ti.task_id else null end) as send_num, --已下发数量
                            count(distinct case when ti.task_state = '2' then ti.task_id else null end) as complete_num, --已完成数量
                            count(distinct case when ti.task_state = '3' then ti.task_id else null end) as overtime_not_feedback_num, --超时未反馈数量
                            count(distinct case when ti.feedback_num = ti.all_num and ti.all_num > 0 then ti.task_id else null end),-- 已反馈
                            count(distinct case when ti.feedback_num < ti.all_num and ti.all_num > 0 then ti.task_id else null end),--未反馈
                            round(case when count(distinct ti.task_id) ='0' then 0
                                    else count(distinct case when ti.task_state = '2' then ti.task_id else null end)*1.0/count(distinct ti.task_id) end*100,2)||'%%' as task_feedback_pre
                    from (
                            select *,regexp_split_to_table(regexp_split_to_table(aim_ids,','),'/') as aim_id
                            from temp_attackevent_detail1
                            where %s
                    ) ti %s group by static_date order by static_date
                """ % (strcustomize, strtype))
                data['tableCollection'] = g.cursor.fetchall()
            elif data['reportType'] == 'week':
                g.cursor.execute("""
                    select a.first_data as fromdate, a.lastdate as todate
                    from (
                        select substring(generate_series(date_trunc('week',a.min_static_date), date_trunc('week',a.max_static_date),'1 week')::varchar from 0 for 11) as first_data,
                            substring(d2t(generate_series(date_trunc('week',a.min_static_date), date_trunc('week',a.max_static_date),'1 week')::date+6)from 0 for 11) as lastdate
                        from (
                            select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                                substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                            from t_demand_monitor_statistics where aim_ids='9999' and task_type='9999'
                        )a
                    )a
                """)
                # print g.cursor.fetchall()
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '按需采集统计周报'
            elif data['reportType'] == 'month':
                g.cursor.execute("""
                select substring(a.first_data::varchar from 0 for 11) as fromdate,substring(a.lastdate::varchar from 0 for 11) as todate
                from (
                    select generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month') as first_data,
                        generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month')::timestamp + '1 month - 1 day' as lastdate
                    from (
                        select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                            substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                        from t_demand_monitor_statistics where aim_ids='9999' and task_type='9999'
                    )a
                )a
                """)
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '按需采集统计月报'
            elif data['reportType'] == 'period':
                g.cursor.execute("""
                select substring(a.first_data::varchar from 0 for 11) as fromdate,substring(a.lastdate::varchar from 0 for 11) as todate
                from (
                    select generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'3 month') as first_data,
                            generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'3 month')::timestamp + '3 month - 1 day' as lastdate
                    from (
                        select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                            substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                        from t_demand_monitor_statistics where  aim_ids='9999' and task_type='9999'
                    )a
                )a
                left join (
                    select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date
                    from t_demand_monitor_statistics where  aim_ids='9999' and task_type='9999'
                )b
                on 1=1
                where a.lastdate>b.min_static_date
                """)
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '按需采集统计季报'
            else:
                g.cursor.execute("""
                select substring(a.first_data::varchar from 0 for 11) as fromdate,substring(a.lastdate::varchar from 0 for 11) as todate
                from (
                    select generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'1 year') as first_data,
                            generate_series(date_trunc('year',a.min_static_date), date_trunc('month',a.max_static_date),'1 year')::timestamp + '1 year' as lastdate
                    from (
                        select substring(min(static_date)::varchar from 0 for 11)::timestamp as min_static_date,
                            substring(max(static_date)::varchar from 0 for 11)::timestamp as max_static_date
                        from t_demand_monitor_statistics where aim_ids='9999' and task_type='9999'
                    )a
                )a
                """)
                data['tablelist'] = g.cursor.fetchall()
                data['name'] = '按需采集统计年报'
        return json.dumps({"status": "success", "data": data})
    except Exception as e:
        return json.dumps({"status": "except", "msg": str(e)})


# /api/safetyformlls/submitcondition
@app.route('/api/safetyForm/safetybasicdata', methods=['post'])
@auth.permission('safetyForm')
def safetyformlls_basic_data():
    try:
        g.cursor.execute("""select agency_id as value,
                            agency_name as label
                            from sys_agency_info_fdw
                            where state = '1' and node_type_id='1' and agency_id !='0' """)
        Scopetask = g.cursor.fetchall()
        g.cursor.execute("""select
                                distinct a.victim_unit as label,
                                a.node_id as value
                            from h_victim_info_fdw as a""")
        cause = g.cursor.fetchall()
        return json.dumps({
            "status": "success",
            "data": {
                "Scopetask": Scopetask,
                "cause": cause
            }})
    except Exception as e:
        return json.dumps({"status": "except", "msg": str(e)})


@app.route('/api/safereport/checkdate', methods=['POST'])
@auth.permission('safetyForm')
def checkdate():
    try:
        form = ValidateForm(
            fromdate=['起始时间', V.required()],
            todate=['终止时间', V.required()],
            name=['报告名称', V.required()]
        )
        (flag, data) = form.validate()
        if not flag:
            data['status'] = 'fail'
            return json.dumps(data)
        strlist = ''
        if data['fromdate']:
            strlist = "and '%s'<=static_date and static_date<='%s'" % (data['fromdate'], data['todate'])
        else:
            strlist = ''
        if data['name'] == '攻击事件统计周报' or data['name'] == '攻击事件统计月报':
            g.cursor.execute("""
                        SELECT substring(static_date::varchar from 0 for 11) as static_date,victim_system_num, victim_assert_num,
                            event_num, add_num, already_analysis_num, not_analysis_num, handle_pre
                        FROM public.t_attackevent_statictis
                        where node_id='9999' and threaten_level='9999' %s order by static_date
                    """ % strlist)
            data['tableAttack'] = g.cursor.fetchall()
        elif data['name'] == '攻击事件统计季报' or data['name'] == '攻击事件统计年报':
            g.cursor.execute("""
                select substring(b.static_date::varchar from 0 for 8) as static_date, victim_system_num, victim_assert_num,
                        event_num, add_num, already_analysis_num, not_analysis_num, handle_pre
                from (
                    select a.first_data,a.last_date,max(static_date) as static_date
                    from (
                        select generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month') as first_data,
                                generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month')::timestamp + '1 month - 1 day' as last_date
                                from (
                                select %(fromdate)s::timestamp as min_static_date,
                                    %(todate)s::timestamp as max_static_date
                                )a
                    )a
                    left join (
                        select TO_CHAR(static_date, 'YYYY-MM-DD')::timestamp as static_date
                        from t_attackevent_statictis
                        where node_id='9999' and threaten_level='9999'
                    ) b
                    on b.static_date between a.first_data and a.last_date
                    group by a.first_data,a.last_date
                )a
                left join (
                    select TO_CHAR(static_date, 'YYYY-MM-DD')::timestamp as static_date, victim_system_num, victim_assert_num,
                        event_num, add_num, already_analysis_num, not_analysis_num, handle_pre
                    FROM public.t_attackevent_statictis
                    where node_id='9999' and threaten_level='9999'
                ) b
                on a.static_date =b.static_date
                where a.static_date is not null
                order by b.static_date
            """, data)
            data['tableAttack'] = g.cursor.fetchall()
        if data['name'] == '风险事项统计周报' or data['name'] == '风险事项统计月报':
            g.cursor.execute("""
                        SELECT substring(static_date::varchar from 0 for 11) as static_date, riskitem_num, add_num, alwarning_num, not_warning_num,
                            handle_pre
                        FROM public.t_riskitem_statistics
                        where risk_event_level='9999' %s order by static_date
                    """ % strlist)
            data['tableRisk'] = g.cursor.fetchall()
        elif data['name'] == '风险事项统计季报' or data['name'] == '风险事项统计年报':
            g.cursor.execute("""
                select substring(b.static_date::varchar from 0 for 8) as static_date, riskitem_num, add_num, alwarning_num, not_warning_num,
                    handle_pre
                from (
                    select a.first_data,a.last_date,max(static_date) as static_date
                    from (
                        select generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month') as first_data,
                                generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month')::timestamp + '1 month - 1 day' as last_date
                                from (
                                select %(fromdate)s::timestamp as min_static_date,
                                    %(todate)s::timestamp as max_static_date
                                )a
                    )a
                    left join (
                        select TO_CHAR(static_date, 'YYYY-MM-DD')::timestamp as static_date
                        from t_riskitem_statistics
                        where risk_event_level='9999'
                    ) b
                    on b.static_date between a.first_data and a.last_date
                    group by a.first_data,a.last_date
                )a
                left join (
                    select TO_CHAR(static_date, 'YYYY-MM-DD')::timestamp as static_date, riskitem_num, add_num, alwarning_num, not_warning_num,
                        handle_pre
                    FROM public.t_riskitem_statistics
                    where risk_event_level='9999'
                ) b
                on a.static_date =b.static_date
                where a.static_date is not null
                order by b.static_date
            """, data)
            data['tableRisk'] = g.cursor.fetchall()
        if data['name'] == '按需采集统计周报' or data['name'] == '按需采集统计月报':
            g.cursor.execute("""
                        SELECT substring(static_date::varchar from 0 for 11) as static_date, task_num, deep_analysis_num, flow_monitor_num,
                            audit_monitor_num, frail_monitor_num, send_num, complete_num,
                            overtime_not_feedback_num, feedback_num, not_feedback_num, task_feedback_pre
                        FROM public.t_demand_monitor_statistics
                        where aim_ids='9999' and task_type='9999' %s order by static_date
                    """ % strlist)
            data['tableCollection'] = g.cursor.fetchall()
        elif data['name'] == '按需采集统计季报' or data['name'] == '按需采集统计年报':
            g.cursor.execute("""
                select substring(b.static_date::varchar from 0 for 8) as static_date, task_num, deep_analysis_num, flow_monitor_num,
                        audit_monitor_num, frail_monitor_num, send_num, complete_num,
                        overtime_not_feedback_num, feedback_num, not_feedback_num, task_feedback_pre
                from (
                    select a.first_data,a.last_date,max(static_date) as static_date
                    from (
                        select generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month') as first_data,
                                generate_series(date_trunc('month',a.min_static_date), date_trunc('month',a.max_static_date),'1 month')::timestamp + '1 month - 1 day' as last_date
                                from (
                                select %(fromdate)s::timestamp as min_static_date,
                                    %(todate)s::timestamp as max_static_date
                                )a
                    )a
                    left join (
                        select TO_CHAR(static_date, 'YYYY-MM-DD')::timestamp as static_date
                        from t_demand_monitor_statistics
                        where aim_ids='9999' and task_type='9999'
                    ) b
                    on b.static_date between a.first_data and a.last_date
                    group by a.first_data,a.last_date
                )a
                left join (
                    select TO_CHAR(static_date, 'YYYY-MM-DD')::timestamp as static_date, task_num, deep_analysis_num, flow_monitor_num,
                        audit_monitor_num, frail_monitor_num, send_num, complete_num,
                        overtime_not_feedback_num, feedback_num, not_feedback_num, task_feedback_pre
                    from t_demand_monitor_statistics
                    where aim_ids='9999' and task_type='9999'
                ) b
                on a.static_date =b.static_date
                where a.static_date is not null
                order by b.static_date
            """, data)
            data['tableCollection'] = g.cursor.fetchall()
        return json.dumps({'status': 'success', 'data': data})
    except Exception as e:
        return json.dumps({'status': 'except', 'msg': str(e)})
